
import GUI.Multilang.MyResources;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Locale;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class frmClient extends JFrame implements ActionListener
{

    private Locale _currentLocale;
    private Container _contentPane;
    private JLabel _lblStatus;
    private JTabbedPane _mainTabPanel;
    private boolean _connected;
    public MyQueryPanel _queryPanel;
    public static final Locale[] SupportedLanguage =
    {
        Locale.ENGLISH,
        new Locale("vi", "VI")
    };

    public frmClient()
    {
        super("SQL Client_0912_212_224_346");
        _currentLocale = SupportedLanguage[1];
        InitComponents();
        _connected = true;
        Disconnect();
        Connect();
    }

    private void InitComponents()
    {
        MyResources res = new MyResources(_currentLocale);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        _contentPane = new Container();
        this.setContentPane(_contentPane);
        _contentPane.setLayout(new BorderLayout());
        _contentPane.setPreferredSize(new Dimension(450, 130));
        InitMenuBar();
        InitToolBar();
        InitCenterContent();
        InitFooter();

        setResizable(true);
        setSize(900, 600);
        setVisible(true);
        //center jframe
        this.setLocationRelativeTo(null);
    }
    private JMenuItem _mnuNewQuery;

    private void InitMenuBar()
    {
        Font font = new Font("Times New Roman", Font.PLAIN, 15);
        JMenuBar menuBar = new JMenuBar();

        menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

        JMenuItem menuItem;

        //Menu File
        JMenu menuFile = new JMenu(MyResources.GetString("file"));
        menuFile.setFont(font);
        menuFile.setMnemonic(KeyEvent.VK_F);

        menuItem = new JMenuItem(MyResources.GetString("connect"), getIcon("iconConnect"));
        menuItem.setActionCommand("Connect");
        menuItem.addActionListener(this);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        menuFile.add(menuItem);

        menuItem = new JMenuItem(MyResources.GetString("disconnect"), getIcon("iconDisconnect"));
        menuItem.setActionCommand("Disconnect");
        menuItem.addActionListener(this);
        menuFile.add(menuItem);
        menuFile.addSeparator();

        JMenu menuFileNew = new JMenu(MyResources.GetString("new"));
        menuFileNew.setIcon(getIcon("iconNew"));
        menuFileNew.setMnemonic(KeyEvent.VK_N);

        _mnuNewQuery = new JMenuItem(MyResources.GetString("sqlquery"), getIcon("iconNewQuery"));
        _mnuNewQuery.setActionCommand("NewQuery");
        _mnuNewQuery.addActionListener(this);
        menuFile.add(_mnuNewQuery);
        _mnuNewQuery.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));

        menuItem = new JMenuItem(MyResources.GetString("exit"), getIcon("iconExit"));
        menuItem.setActionCommand("Exit");
        menuItem.addActionListener(this);
        menuFile.add(menuItem);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));

        menuBar.add(menuFile);

        //Menu Language
        JMenu menuLanguage = new JMenu(MyResources.GetString("language"));
        menuBar.add(menuLanguage);
        menuItem = new JMenuItem("English");
        menuLanguage.add(menuItem);
        menuItem.addActionListener(this);
        menuItem.setActionCommand("LanguageEnglish");
        menuItem = new JMenuItem("Tiếng Việt");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("LanguageVietnamese");
        menuLanguage.add(menuItem);


        //Menu Help
        //Menu Help
        JMenu menuHelp = new JMenu(MyResources.GetString("help"));
        menuHelp.setMnemonic(KeyEvent.VK_H);
        menuItem = new JMenuItem(MyResources.GetString("about"), getIcon("iconAbout"));
        menuItem.addActionListener(this);
        menuItem.setActionCommand("About");
        menuHelp.add(menuItem);
        menuHelp.setMnemonic(KeyEvent.VK_H);
        menuBar.add(menuHelp);

        this.setJMenuBar(menuBar);
    }

    private ImageIcon getIcon(String str)
    {
        String path = "/Res/" + str + ".png";
        try
        {
            ImageIcon icon = new ImageIcon(this.getClass().getResource(path));

            return icon;
        }
        catch (Exception ex)
        {
            return new ImageIcon();
        }
    }
    JButton _btnNewQuery;

    private void InitToolBar()
    {
        JToolBar toolBar = new JToolBar(MyResources.GetString("toolbar"));
        JToolbarSeparator sp1 = new JToolbarSeparator();
        JToolbarSeparator sp2 = new JToolbarSeparator();

        JButton button = makeNavigationButton(MyResources.GetString("connect"), "Connect", "iconConnect.png");
        button.setToolTipText(MyResources.GetString("connect"));
        toolBar.add(button);

        button = makeNavigationButton(MyResources.GetString("disconnect"), "Disconnect", "iconDisconnect.png");
        button.setToolTipText(MyResources.GetString("disconnect"));
        toolBar.add(button);
        toolBar.add(sp1);

        button = makeNavigationButton(MyResources.GetString("newquery"), "NewQuery", "iconNewQuery.png");
        button.setToolTipText(MyResources.GetString("newquery"));
        toolBar.add(button);
        toolBar.add(sp2);

        _btnNewQuery = button;
        _contentPane.add(toolBar, BorderLayout.PAGE_START);
    }

    private JButton makeNavigationButton(String altText, String actionCommand, String pathIcon)
    {
        String pathIC = "Res/" + pathIcon;
        ImageIcon icon = new ImageIcon(this.getClass().getResource(pathIC));
        JButton button = new JButton();
        button.setIcon(icon);
        button.setBorderPainted(false);
        button.setOpaque(false);
        button.setActionCommand(actionCommand);
        button.addActionListener(this);
        return button;
    }

     JPanel _mainPanel;
    private void InitCenterContent()
    {
        _mainPanel = new JPanel(new BorderLayout());
        _mainPanel.setOpaque(true);
        _mainPanel.setMinimumSize(new Dimension(450, 50));
        _mainTabPanel = new JTabbedPane();
        _mainTabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        _mainPanel.add(_mainTabPanel);
        _contentPane.add(_mainPanel, BorderLayout.CENTER);
    }

    private void InitFooter()
    {
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setOpaque(true);
        mainPanel.setPreferredSize(new Dimension(900, 30));
        _lblStatus = new JLabel("Status");
        mainPanel.add(_lblStatus);
        _contentPane.add(mainPanel, BorderLayout.PAGE_END);
    }

    public JPanel AddTab(String name, JPanel panel)
    {

        ArrayList<String> tabsname = new ArrayList();
        for (int i = 0; i < _mainTabPanel.getTabCount(); i++)
        {
            tabsname.add(_mainTabPanel.getTitleAt(i));
        }

        int index = tabsname.indexOf(name);

        if (index < 0)
        {
            _mainTabPanel.addTab(name, panel);
            index = _mainTabPanel.indexOfComponent(panel);
            _mainTabPanel.setTabComponentAt(index, new ButtonTabComponent(_mainTabPanel));
        }
        _mainTabPanel.setSelectedIndex(index);
        return (JPanel) _mainTabPanel.getComponentAt(index);
    }

    private void SetStatus(String text)
    {
        _lblStatus.setText(text);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        switch (actionCommand)
        {
            case "LanguageEnglish":
                _currentLocale = SupportedLanguage[0];
                MyResources.SetLocale(_currentLocale);
                try
                {
                    this.InitComponents();
                    SetStatus("Language: English");
                }
                catch (Exception ex)
                {
                }
                break;
            case "LanguageVietnamese":
                _currentLocale = SupportedLanguage[1];
                MyResources.SetLocale(_currentLocale);
                try
                {
                    this.InitComponents();
                    SetStatus("Ngôn ngữ: Tiếng Việt");
                }
                catch (Exception ex)
                {
                }
                break;

            case "NewQuery":
                _queryPanel = new MyQueryPanel(this);
                AddTab(MyResources.GetString("query"), _queryPanel);
                break;

            case "Exit":
                System.exit(0);
                break;
            case "About":
                ShowAboutScreen();
                break;
            case "Connect":
                Connect();
                break;
            case "Disconnect":
                Disconnect();
                break;
            default:
                break;
        }
    }

    private void ShowAboutScreen()
    {
        final JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(new Dimension(500, 350));
        frame.setAutoRequestFocus(false);
        frame.setModalExclusionType(null);
        frame.setResizable(false);
        frame.setType(Window.Type.UTILITY);
        frame.setUndecorated(true);
        JLabel lbl = new JLabel();
        lbl.setIcon(getIcon("About_Client"));
        frame.add(lbl);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent e)
            {
                frame.dispose();
            }
        });
    }

    private void Disconnect()
    {


        if (_connected)
        {
            _connected = false;
            _mainTabPanel.removeAll();
            _mnuNewQuery.setEnabled(false);
            _btnNewQuery.setEnabled(false);
            SetStatus(MyResources.GetString("disconnected"));
        }
    }

    private void Connect()
    {
        if (!_connected)
        {
            DialogConnectToServer dcs = new DialogConnectToServer(this);
            dcs.setVisible(true);
        }
    }

    public void SetConnected(MyConnectionString connectionString)
    {
        _connected = true;
        _mnuNewQuery.setEnabled(true);
        _btnNewQuery.setEnabled(true);
        SetStatus(MyResources.GetString("connected"));
        client = new Client(this, connectionString);
        Thread t = new Thread(client);
        t.start();
    }
    public Client client;
}



class JToolbarSeparator extends JComponent
{

    /**
     * Creates a new JToolbarSeparator object.
     */
    public JToolbarSeparator()
    {
        setMaximumSize(new Dimension(5, Integer.MAX_VALUE));
    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Dimension size = getSize();
        int pos = size.width / 2;
        g.setColor(Color.GRAY);
        g.drawLine(pos, 3, pos, size.height - 5);
        g.drawLine(pos, 2, pos + 1, 2);
        g.setColor(Color.white);
        g.drawLine(pos + 1, 3, pos + 1, size.height - 5);
        g.drawLine(pos, size.height - 4, pos + 1, size.height - 4);
    }
}
